package com.jie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jie.dao.CartDao;
import com.jie.dao.GoodsDao;
import com.jie.entity.Goods;
import com.jie.entity.ShoppingCart;
import com.jie.mapper.ShoppingCartMapper;
import com.jie.service.GoodsService;
import com.jie.service.ShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.utility.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jie
 * @since 2024-03-15
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper cartMapper;
    @Autowired
    private GoodsService goodsService;
    @Override
    public List<CartDao> allCart(Integer userId) {
        List<CartDao> cartDaos=new ArrayList<>();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = cartMapper.selectList(queryWrapper);
        if(shoppingCarts.size()<0){
            return cartDaos;
        }
        for (ShoppingCart shoppingCart :shoppingCarts){
            R<GoodsDao> goodsDaoR = goodsService.goodsByid(shoppingCart.getGoodsId());
            GoodsDao goodsDao=goodsDaoR.getData();
            CartDao cartDao=new CartDao();
            BeanUtils.copyProperties(goodsDao,cartDao);
            cartDao.setShoppingId(shoppingCart.getShoppingId());
            cartDao.setNumber(shoppingCart.getNumber());
            cartDao.setTotalPrice(shoppingCart.getTotalPrice());
            cartDao.setUserId(shoppingCart.getUserId());
            cartDao.setManyAttrsIndex(shoppingCart.getManyAttrsIndex());
            cartDao.setChecked(shoppingCart.getChecked());
            cartDaos.add(cartDao);
        }
        return cartDaos;
    }
}
